import { getAllOrder, itemOrder } from '../../api/apis.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switches: [
      {
        text: '项目订单',
        state: 'project',
        selected: true
      },
      {
        text: '商品订单',
        state: 'product',
        selected: false
      }
    ],
    orderList: [],
    projectList: [],
    switchState: 'project',
    pageIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._initData()
  },
  _initData() {
    this._getList()
  },
  _getList() {
    switch (this.data.switchState) {
      case 'project':
        this._getProjectOrder()
        break
      case 'product':
        this._getProductOrder()
    }
  },
  _getProductOrder() {
    getAllOrder({
      data: {
        index: ++this.data.pageIndex,
        size: 50,
        openid: app.globalData.openid,
        type: 'all'
      },
      success: res => {
        if (res.state) {
          if (res.data.length>0) {
            this.setData({
              orderList: this.data.orderList.concat(res.data)
            })
          } else {
            this.setData({
              noData: true
            })
          }
        }
      }
    })
  },
  _getProjectOrder() {
    itemOrder({
      data: {
        index: ++this.data.pageIndex,
        size: 50,
        openid: app.globalData.openid,
      },
      success: res => {
        if (res.state) {
          if (res.data.list.length > 0) {
            this.setData({
              projectList: this.data.projectList.concat(res.data.list)
            })
          } else {
            this.setData({
              noData: true
            })
          }
        }
      }
    })
  },
  _switch(e) {
    this.data.switches.map((item, index) => {
      if (e.currentTarget.dataset.index === index) {
        item.selected = true
        this.setData({
          switchState: item.state
        })
      } else {
        item.selected = false
      }
      return item
    })
    this.setData({
      switches: this.data.switches,
      noData: false,
      orderList: [],
      projectList: [],
      pageIndex: 0
    })
    this._getList()
  },
  setCopy(e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.copy,
      success: res => {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        })
      },
      fail: res => {
        wx.showToast({
          title: '复制失败',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this._getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})